package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppSpouseClerk;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Repository
public class AppSpouseClerkDao extends BaseDao<AppSpouseClerk> {

    public AppSpouseClerk randomGet(Integer sex)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "select * from app_spouse_clerk where sex=? and status=1";
        list.add(sex);
        List<AppSpouseClerk> appSpouseClerks = this.queryList(sql, list, AppSpouseClerk.class);
        if(appSpouseClerks!=null && appSpouseClerks.size()>0){
            Random random = new Random();
            AppSpouseClerk appSpouseClerk = appSpouseClerks.get(random.nextInt(appSpouseClerks.size()));
            return appSpouseClerk;
        }else{
            return null;
        }
    }


    public List<AppSpouseClerk> getList(SplitPageBean splitPageBean, AppSpouseClerk appSpouseClerk)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "select * from app_spouse_clerk where 1=1";
        if(!StringEx.isNullOrLing(appSpouseClerk.getStatus())){
            sql += " and status=?";
            list.add(appSpouseClerk.getStatus());
        }
        if(!StringEx.isNullOrLing(appSpouseClerk.getSex())){
            sql += " and sex=?";
            list.add(appSpouseClerk.getSex());
        }
        if(StringEx.isNotNull(appSpouseClerk.getBeginTime()) && StringEx.isNotNull(appSpouseClerk.getEndTime())){
            sql += " and createtime>=? and createtime<=?";
            list.add(appSpouseClerk.getBeginTime());
            list.add(appSpouseClerk.getEndTime());
        }
        sql += " order by status asc,createtime desc";
        return this.queryList(sql,list,AppSpouseClerk.class,splitPageBean);
    }


    public void addOrEdit(AppSpouseClerk appSpouseClerk)
    {
        if(StringEx.isNullOrLing(appSpouseClerk.getId())){
            appSpouseClerk.setCreatetime(new Date());
            appSpouseClerk.setUpdatetime(new Date());
            this.add(appSpouseClerk);
        }else{
            appSpouseClerk.setUpdatetime(new Date());
            this.update(appSpouseClerk);
        }
    }


}
